<#import "../tpl/pageTep.ftl" as page>
<@page.pageBase currentMenu="文章管理">
<style>
    .form-horizontal .controls {
        line-height: 40px;
        height: 40px;
    }

    .button-primary, .button-success, .button-danger {
        margin-left: 5px;
    }
</style>
<form id="searchForm" class="form-panel">
    <ul class="panel-content">
        <li>
            <div class="control-group span5">
                <div class="controls">
                    <input type="text" name="title" id="title" value="" placeholder="输入文章标题">
                </div>
            </div>
            <div class="form-actions span8">
                <button type="submit" class="button  button-primary">
                    搜索
                </button>
                <a href="${basepath}/manage/news/toAdd" class="button button-success">新增</a>
                <a class="button  button-danger" id="delBtn">
                    删除
                </a>
            </div>
        </li>
    </ul>
</form>
<div id="grid"></div>
<script>
    $.ajax({
        url: "/getSession",
        dataType: "json",
        type: "GET",
        success: function (data) {
            var columns;

            if (data.data.organize == null) {
                columns = [
                    {title: 'ID', dataIndex: 'id', width: '10%', elCls: 'center'},
                    {
                        title: '发布人',
                        dataIndex: 'organizeName',
                        width: '10%',
                        elCls: 'center',
                        renderer: function (data) {
                            if (data == null) {
                                return "爱优享平台";
                            } else {
                                return data;
                            }
                        }
                    },
                    {title: '标题', dataIndex: 'title', width: "40%", elCls: 'center'},
                    {title: '最后操作时间', dataIndex: 'updatetime', width: "20%", elCls: 'center'},
                    {
                        title: '显示状态', dataIndex: 'status', elCls: 'center', width: "20%", renderer: function (data) {
                        if (data == "y") {
                            return '<img src="${basepath}/resource/images/action_check.gif">';
                        } else {
                            return '<img src="${basepath}/resource/images/action_delete.gif">';
                        }
                    }
                    },
                    {
                        title: '操作', dataIndex: 'id', elCls: 'center', width: "20%", renderer: function (value, obj) {
                        if (obj.type != "o")
                            return '<a href="${basepath}/manage/news/toEdit?id=' + value + '">编辑</a>';
                        else
                            return '<a href="${basepath}/manage/news/toView?id=' + value + '">查看</a>';
                    }
                    }
                ]
            } else {
                columns = [
                    {title: 'ID', dataIndex: 'id', width: '10%', elCls: 'center'},
                    {
                        title: '发布人',
                        dataIndex: 'organizeName',
                        width: '10%',
                        elCls: 'center',
                        renderer: function (data) {
                            if (data == null) {
                                return "爱优享平台";
                            } else {
                                return data;
                            }
                        }
                    },
                    {title: '标题', dataIndex: 'title', width: "40%", elCls: 'center'},

                    {title: '最后操作时间', dataIndex: 'updatetime', width: "20%", elCls: 'center'},
                    {
                        title: '显示状态', dataIndex: 'status', elCls: 'center', width: "20%", renderer: function (data) {
                        if (data == "y") {
                            return '<img src="${basepath}/resource/images/action_check.gif">';
                        } else {
                            return '<img src="${basepath}/resource/images/action_delete.gif">';
                        }
                    }
                    },
                    {
                        title: '审批状态',
                        dataIndex: 'approvalStatus',
                        width: "20%",
                        elCls: 'center hidden',
                        renderer: function (data) {
                            switch (data) {
                                case "init":
                                    return '待审核';
                                case "pass":
                                    return '已通过';
                                case "fail":
                                    return '不通过';
                                default:
                                    return "失效";
                            }
                        }
                    },
                    {
                        title: '操作', dataIndex: 'id', elCls: 'center', width: "20%", renderer: function (value, obj) {
                        if (obj.approvalStatus != "init" && obj.approvalStatus != "pass")
                            return '<a href="${basepath}/manage/news/toEdit?id=' + value + '&type=reopen">编辑</a>';
                        else
                            return '<a href="${basepath}/manage/news/toView?id=' + value + '">查看</a>';
                    }
                    }
                ]
            }
            var Grid = BUI.Grid,
                    Store = BUI.Data.Store;
            var store = new Store({
                        url: 'loadData',
                        autoLoad: true, //自动加载数据
                        params: { //配置初始请求的参数
                            length: '10',
                            status: $("#status").val()
                        },
                        pageSize: 10,	// 配置分页数目
                        root: 'list',//统一格式
                        totalProperty: 'total'//统一格式
                    }),
                    grid = new Grid.Grid({
                        render: '#grid',
                        columns: columns,
                        loadMask: true, //加载数据时显示屏蔽层
                        store: store,
                        plugins: [Grid.Plugins.CheckSelection], // 插件形式引入多选表格

                        // 底部工具栏
                        bbar: {
                            pagingBar: true
                        }
                    });
            grid.render();//渲染

            var form = new BUI.Form.HForm({
                srcNode: '#searchForm'
            }).render();

            form.on('beforesubmit', function (ev) {
                //序列化成对象
                var obj = form.serializeToObject();
                obj.start = 0; //返回第一页
                store.load(obj);
                return false;
            });
            //删除选中的记录

            function delFunction() {
                var selections = grid.getSelection();
                var ids = new Array();
                for (var i = 0; i < selections.length; i++) {
                    ids[i] = selections[i].id.toString()
                }
                $.ajax({
                    type: "POST",
                    url: "${basepath}/manage/news/deletesJson",//批量删除
                    dataType: "json",
                    data: {
                        ids: ids
                    },
                    success: function (data) {
                        var obj = form.serializeToObject();
                        obj.start = 0; //返回第一页
                        store.load(obj);
                    }
                });
            }
            $("#delBtn").click(function(){
                delFunction();
            })
        }
    });


</script>
</@page.pageBase>